package cn.shlbysj.demo.controller;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.DiscusInfo;
import cn.shlbysj.demo.service.IArticleService;
import cn.shlbysj.demo.service.IDiscusInfoService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/article")
@Api("毕设文章模块")
public class ArticleController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IDiscusInfoService discusInfoService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取文章列表接口")
    @ResponseBody
    public Result list(HttpServletRequest request,
                       @ApiParam(name="title",value="毕设标题")@RequestParam(value = "title",required = false) String title,
                       @ApiParam(name="type",value="类型")@RequestParam(value = "type",required = false) Integer type,
                       @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                       @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = articleService.findPage(title,type,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }


    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "发布文章")
    @ResponseBody
    public Result insert(HttpServletRequest request,
                       @ApiParam(name="artInfo",value="毕设信息")@RequestBody ArtInfo artInfo
    ){
        try {
            return articleService.insert(artInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "修改文章")
    @ResponseBody
    public Result update(HttpServletRequest request,
                         @ApiParam(name="artInfo",value="毕设信息")@RequestBody ArtInfo artInfo
    ){
        try {
            return articleService.update(artInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "删除文章")
    @ResponseBody
    public Result delete(HttpServletRequest request,
                         @ApiParam(name="artId",value="毕设主键ID",required = true)@RequestParam String artId
    ){
        try {
            return articleService.delete(artId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/getDiscussList",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取毕设评论分页列表")
    @ResponseBody
    public Result list(HttpServletRequest request,
                       @ApiParam(name="artId",value="毕设ID",required = true)@RequestParam(value = "artId",required = false) String artId,
                       @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                       @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = discusInfoService.findPage(artId,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/discuss/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "发布评论信息")
    @ResponseBody
    public Result discussInsert(HttpServletRequest request,
                         @ApiParam(name="artInfo",value="评论信息")@RequestBody DiscusInfo discusInfo
    ){
        try {
            return discusInfoService.insert(discusInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/discuss/update",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "修改评论信息")
    @ResponseBody
    public Result update(HttpServletRequest request,
                         @ApiParam(name="artInfo",value="评论信息")@RequestBody DiscusInfo discusInfo
    ){
        try {
            return discusInfoService.update(discusInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
}
